<template>
  <!-- 角色逻辑主要在GameStage中处理 -->
</template>

<script>
import { ref, reactive } from 'vue'
import { useControls } from '../composables/useControls'
import { useAnimations } from '../composables/useAnimations'

export default {
  props: {
    model: Object,
    isPlayer1: Boolean,
    position: Object,
    rotation: Object
  },
  setup(props) {
    const state = reactive({
      health: 100,
      position: props.position,
      rotation: props.rotation,
      velocity: { x: 0, y: 0, z: 0 },
      state: 'idle',
      attacks: [],
      comboCount: 0,
      lastAttackTime: 0
    })
    
    const { setupControls } = useControls()
    const { playAnimation } = useAnimations()
    
    // 初始化控制
    const controls = setupControls(props.isPlayer1)
    
    // 更新角色状态
    const update = (delta) => {
      // 处理输入
      handleInput(controls)
      
      // 更新位置
      state.position.x += state.velocity.x * delta
      state.position.z += state.velocity.z * delta
      
      // 确保角色不会超出边界
      state.position.x = Math.max(-14, Math.min(14, state.position.x))
      state.position.z = Math.max(-8, Math.min(8, state.position.z))
      
      // 更新模型位置和旋转
      if (props.model) {
        props.model.position.set(state.position.x, state.position.y, state.position.z)
        props.model.rotation.y = state.rotation.y
      }
    }
    
    // 处理输入
    const handleInput = (controls) => {
      if (state.state === 'hit' || state.state === 'knockdown') return
      
      // 移动
      if (controls.left) {
        state.velocity.x = -5
        state.rotation.y = props.isPlayer1 ? Math.PI / 2 : -Math.PI / 2
      } else if (controls.right) {
        state.velocity.x = 5
        state.rotation.y = props.isPlayer1 ? -Math.PI / 2 : Math.PI / 2
      } else {
        state.velocity.x = 0
      }
      
      // 跳跃
      if (controls.up && state.velocity.y === 0) {
        state.velocity.y = 10
        playAnimation('jump')
      }
      
      // 攻击
      if (controls.attack1) {
        performAttack('punch')
      } else if (controls.attack2) {
        performAttack('kick')
      } else if (controls.special) {
        performAttack('special')
      }
    }
    
    // 执行攻击
    const performAttack = (type) => {
      if (state.state === 'attack') return
      
      state.state = 'attack'
      let attackData = null
      
      switch (type) {
        case 'punch':
          playAnimation('punch')
          attackData = {
            type: 'punch',
            damage: 10,
            range: 2,
            position: { ...state.position },
            direction: state.rotation.y,
            activeTime: 0.3
          }
          break
        case 'kick':
          playAnimation('kick')
          attackData = {
            type: 'kick',
            damage: 15,
            range: 2.5,
            position: { ...state.position },
            direction: state.rotation.y,
            activeTime: 0.4
          }
          break
        case 'special':
          playAnimation('special')
          attackData = {
            type: 'special',
            damage: 25,
            range: 4,
            position: { ...state.position },
            direction: state.rotation.y,
            activeTime: 0.8
          }
          break
      }
      
      state.attacks.push(attackData)
      setTimeout(() => {
        state.state = 'idle'
        state.attacks = state.attacks.filter(a => a !== attackData)
      }, attackData.activeTime * 1000)
    }
    
    // 受击
    const takeHit = (damage) => {
      state.health -= damage
      if (state.health <= 0) {
        state.health = 0
        playAnimation('knockdown')
        state.state = 'knockdown'
      } else {
        playAnimation('hit')
        state.state = 'hit'
        setTimeout(() => {
          state.state = 'idle'
        }, 500)
      }
    }
    
    return {
      state,
      update,
      takeHit
    }
  }
}
</script>